package com.lenovo.leos.filebrowser.netdisk;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Timer;
import java.util.TimerTask;

import com.lenovo.leos.letools.lenovodata.DownLoadServer;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Region.Op;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;

import com.lenovo.leos.letools.db.FileDownloadEntity;
import java.util.List;

public class NetDiskOpService extends Service {
	private static final String TAG = "NetDiskOpService";

	private static final long DELAY_TIME_AFTER_NO_OP = 1000; // 300000; // 5 min
	private static final long MANAGE_THREAD_SLEEP_TIME = 100;

	// summary
	// class UpdateTimer
	// class TaskThread
	// data: mltInRun
	// data: mltTodo
	// maybe a value to control the max number of the tasks in running

	// notification

	// power manager

	/** the interface used by client */
	public interface IServiceBinder {

		public abstract void addOp(NetDiskOp op);

		public abstract boolean isFinished(long opId);

		public abstract String getResult(long opId);

		public abstract String getStatus(long opId);

		public abstract void setEnableNotify(boolean enable);

		public abstract ArrayList<LoadedData> getProgess(long opId);
	}

	public class NetDiskOpBinder extends Binder implements IServiceBinder {

		// @Override
		public synchronized void addOp(NetDiskOp op) {
			if (null == op) {

			} else {
				mllNewOps.addLast(op);
			}
		}

		// @Override
		public synchronized String getResult(long opId) {
			Log.i(TAG, "==> getResult");

			if (0 > opId) {

				return null;
			} else {
				if (null != mhmDone && 0 < mhmDone.size()) {
					NetDiskOp op = mhmDone.get(opId);
					if (null == op) {

						return null;
					} else {
						return op.getResult();
					}
				} else
					return null;
			}
		}

		public synchronized ArrayList<LoadedData> getProgess(long opId) {
			synchronized (mretRinglist) {
				if (31415 == opId && null != mretnullList
						&& 0 != mretnullList.size()) {
					Log.e(TAG,"getProgess==>31415");
					return mretnullList;
				} else if (31415 == opId) {
					Log.d(TAG, "the mretRinglist is ***============== " + mretRinglist);
					return mretRinglist;
				}  
				Log.i(TAG, "==> getProgess---" + opId);
				if (0 > opId) {
					Log.d(TAG, "the mretnullList is 000============== " + mretnullList);
					return mretnullList;
				} else {
					LoadedData retLoadData = null;
					if (null != mhmRunning && 0 < mhmRunning.size()) {
						Log.i(TAG, "==> getProgess---hava data" + opId);
						LoadNetDiskOp op = (LoadNetDiskOp) mhmRunning.get(opId);
						if (null == op) {
							Log.i(TAG, "==> getProgess---op is null" + opId);
							Log.d(TAG, "the mretnullList is 111============== " + mretnullList);
							return mretnullList;
						} else if (op.getType() == LoadNetDiskOp.Type.download_file) {
							String strFileName = op.getData().getString(
									"filename");
							if (op.getState() == LoadNetDiskOp.State.ready) {
								retLoadData = new LoadedData(strFileName,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.ready,
										LoadedData.tag_worktype.download,
										op.getID());
							} else if (op.getState() == LoadNetDiskOp.State.finish) {
								retLoadData = new LoadedData(strFileName,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.finish,
										LoadedData.tag_worktype.download,
										op.getID());
							} else if (op.getState() == LoadNetDiskOp.State.none) {
								retLoadData = new LoadedData(strFileName,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.none,
										LoadedData.tag_worktype.download,
										op.getID());
							} else if (op.getState() == LoadNetDiskOp.State.pause) {
								retLoadData = new LoadedData(strFileName,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.pause,
										LoadedData.tag_worktype.download,
										op.getID());
							} else if (op.getState() == LoadNetDiskOp.State.run) {
								retLoadData = new LoadedData(strFileName,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.run,
										LoadedData.tag_worktype.download,
										op.getID());
							} else if (op.getState() == LoadNetDiskOp.State.start) {
								retLoadData = new LoadedData(strFileName,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.start,
										LoadedData.tag_worktype.download,
										op.getID());
							} else if (op.getState() == LoadNetDiskOp.State.stop) {
								retLoadData = new LoadedData(strFileName,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.stop,
										LoadedData.tag_worktype.download,
										op.getID());
							} else {

							}

							if (mretRinglist != null
									&& mretRinglist.size() != 0) {
								boolean bhava = false;
								for (int i = 0; i < mretRinglist.size(); i++) {
									if (mretRinglist.get(i).getmOpID() == retLoadData
											.getmOpID()) {
										// updata the exiten item
										mretRinglist.get(i).setDone(
												retLoadData.getDone());
										mretRinglist.get(i).setTotal(
												retLoadData.getTotal());
										mretRinglist.get(i).setState(
												retLoadData.getState());
										bhava = true;
									}
								}
								if (!bhava) {
									// add new item
									mretRinglist.add(retLoadData);
								}
								if (mhmRunning.size() < mretRinglist.size()) {
									for (int i = 0; i < mretRinglist.size(); i++) {
										if (mhmRunning.get(mretRinglist.get(i)
												.getmOpID()) == null) {
											mretRinglist.remove(i);
										}
									}
								}
							} else if (mretRinglist != null
									&& mretRinglist.size() == 0) {
								mretRinglist.add(retLoadData);
							}
							Log.d(TAG, "the mretRinglist is 11============== " + mretRinglist);
							return mretRinglist;

						} else if (op.getType() == LoadNetDiskOp.Type.upload_file) {
							String strFileName = op.getData().getString("Path");
							File file = new File(strFileName);

							String strFile = file.getName();
							if (op.getState() == LoadNetDiskOp.State.ready) {
								retLoadData = new LoadedData(strFile,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.ready,
										LoadedData.tag_worktype.upload,
										op.getID());
							} else if (op.getState() == LoadNetDiskOp.State.finish) {
								retLoadData = new LoadedData(strFile,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.finish,
										LoadedData.tag_worktype.upload,
										op.getID());
							} else if (op.getState() == LoadNetDiskOp.State.none) {
								retLoadData = new LoadedData(strFile,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.none,
										LoadedData.tag_worktype.upload,
										op.getID());
							} else if (op.getState() == LoadNetDiskOp.State.pause) {
								retLoadData = new LoadedData(strFile,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.pause,
										LoadedData.tag_worktype.upload,
										op.getID());
							} else if (op.getState() == LoadNetDiskOp.State.run) {
								retLoadData = new LoadedData(strFile,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.run,
										LoadedData.tag_worktype.upload,
										op.getID());
							} else if (op.getState() == LoadNetDiskOp.State.start) {
								retLoadData = new LoadedData(strFile,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.start,
										LoadedData.tag_worktype.upload,
										op.getID());
							} else if (op.getState() == LoadNetDiskOp.State.stop) {
								retLoadData = new LoadedData(strFile,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.stop,
										LoadedData.tag_worktype.upload,
										op.getID());
							} else {

							}

							if (mretRinglist != null
									&& mretRinglist.size() != 0) {
								boolean bhava = false;
								for (int i = 0; i < mretRinglist.size(); i++) {
									if (mretRinglist.get(i).getmOpID() == retLoadData
											.getmOpID()) {
										// updata the exiten item
										mretRinglist.get(i).setDone(
												retLoadData.getDone());
										mretRinglist.get(i).setTotal(
												retLoadData.getTotal());
										mretRinglist.get(i).setState(
												retLoadData.getState());
										bhava = true;
									}
								}  
								if (!bhava) {
									// add new item
									mretRinglist.add(retLoadData);
								}
								if (mhmRunning.size() < mretRinglist.size()) {
									for (int i = 0; i < mretRinglist.size(); i++) {
										if (mhmRunning.get(mretRinglist.get(i)
												.getmOpID()) == null) {
											mretRinglist.remove(i);
										} 
									}
								}
							} else if (mretRinglist != null
									&& mretRinglist.size() == 0) {
								mretRinglist.add(retLoadData);
							}
							Log.d(TAG, "the mretRinglist is 1111============== " + mretRinglist);
							return mretRinglist;
						} else if (op.getType() == LoadNetDiskOp.Type.download_dir) {
							String strDIrName = op.getData().getString(
									"dirname");
							if (op.getState() == LoadNetDiskOp.State.ready) {
								retLoadData = new LoadedData(strDIrName,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.ready,
										LoadedData.tag_worktype.downloaddir,
										op.getID());
								retLoadData.setType(LoadedData.tag_type.dir);
							} else if (op.getState() == LoadNetDiskOp.State.finish) {
								retLoadData = new LoadedData(strDIrName,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.finish,
										LoadedData.tag_worktype.downloaddir,
										op.getID());
								retLoadData.setType(LoadedData.tag_type.dir);
							} else if (op.getState() == LoadNetDiskOp.State.none) {
								retLoadData = new LoadedData(strDIrName,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.none,
										LoadedData.tag_worktype.downloaddir,
										op.getID());
								retLoadData.setType(LoadedData.tag_type.dir);
							} else if (op.getState() == LoadNetDiskOp.State.pause) {
								retLoadData = new LoadedData(strDIrName,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.pause,
										LoadedData.tag_worktype.downloaddir,
										op.getID());
								retLoadData.setType(LoadedData.tag_type.dir);
							} else if (op.getState() == LoadNetDiskOp.State.run) {
								retLoadData = new LoadedData(strDIrName,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.run,
										LoadedData.tag_worktype.downloaddir,
										op.getID());
								retLoadData.setType(LoadedData.tag_type.dir);
							} else if (op.getState() == LoadNetDiskOp.State.start) {
								retLoadData = new LoadedData(strDIrName,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.start,
										LoadedData.tag_worktype.downloaddir,
										op.getID());
								retLoadData.setType(LoadedData.tag_type.dir);
							} else if (op.getState() == LoadNetDiskOp.State.stop) {
								retLoadData = new LoadedData(strDIrName,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.stop,
										LoadedData.tag_worktype.downloaddir,
										op.getID());
								retLoadData.setType(LoadedData.tag_type.dir);
							} else {

							}

							if (mretRinglist != null
									&& mretRinglist.size() != 0) {
								boolean bhava = false;
								for (int i = 0; i < mretRinglist.size(); i++) {
									if (mretRinglist.get(i).getmOpID() == retLoadData
											.getmOpID()) {
										// updata the exiten item
										mretRinglist.get(i).setDone(
												retLoadData.getDone());
										mretRinglist.get(i).setTotal(
												retLoadData.getTotal());
										mretRinglist.get(i).setState(
												retLoadData.getState());
										bhava = true;
									}
								}
								if (!bhava) {
									// add new item
									mretRinglist.add(retLoadData);
								}
								if (mhmRunning.size() < mretRinglist.size()) {
									for (int i = 0; i < mretRinglist.size(); i++) {
										if (mhmRunning.get(mretRinglist.get(i)
												.getmOpID()) == null) {
											mretRinglist.remove(i);
										}
									}
								}
							} else if (mretRinglist != null
									&& mretRinglist.size() == 0) {
								mretRinglist.add(retLoadData);
							}
							return mretRinglist;
						} else if (op.getType() == LoadNetDiskOp.Type.upload_dir) {
							String strDirWholepath = op.getData().getString(
									"dirWholepath");
							if (op.getState() == LoadNetDiskOp.State.ready) {
								retLoadData = new LoadedData(strDirWholepath,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.ready,
										LoadedData.tag_worktype.uploaddir,
										op.getID());
								retLoadData.setType(LoadedData.tag_type.dir);
							} else if (op.getState() == LoadNetDiskOp.State.finish) {
								retLoadData = new LoadedData(strDirWholepath,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.finish,
										LoadedData.tag_worktype.uploaddir,
										op.getID());
								retLoadData.setType(LoadedData.tag_type.dir);
							} else if (op.getState() == LoadNetDiskOp.State.none) {
								retLoadData = new LoadedData(strDirWholepath,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.none,
										LoadedData.tag_worktype.uploaddir,
										op.getID());
								retLoadData.setType(LoadedData.tag_type.dir);
							} else if (op.getState() == LoadNetDiskOp.State.pause) {
								retLoadData = new LoadedData(strDirWholepath,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.pause,
										LoadedData.tag_worktype.uploaddir,
										op.getID());
								retLoadData.setType(LoadedData.tag_type.dir);
							} else if (op.getState() == LoadNetDiskOp.State.run) {
								retLoadData = new LoadedData(strDirWholepath,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.run,
										LoadedData.tag_worktype.uploaddir,
										op.getID());
								retLoadData.setType(LoadedData.tag_type.dir);
							} else if (op.getState() == LoadNetDiskOp.State.start) {
								retLoadData = new LoadedData(strDirWholepath,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.start,
										LoadedData.tag_worktype.uploaddir,
										op.getID());
								retLoadData.setType(LoadedData.tag_type.dir);
							} else if (op.getState() == LoadNetDiskOp.State.stop) {
								retLoadData = new LoadedData(strDirWholepath,
										op.getTotalLength(), op.getProgress(),
										LoadedData.tag_state.stop,
										LoadedData.tag_worktype.uploaddir,
										op.getID());
								retLoadData.setType(LoadedData.tag_type.dir);
							} else {

							}

							if (mretRinglist != null
									&& mretRinglist.size() != 0) {
								boolean bhava = false;
								for (int i = 0; i < mretRinglist.size(); i++) {
									if (mretRinglist.get(i).getmOpID() == retLoadData
											.getmOpID()) {
										// updata the exiten item
										mretRinglist.get(i).setDone(
												retLoadData.getDone());
										mretRinglist.get(i).setTotal(
												retLoadData.getTotal());
										mretRinglist.get(i).setState(
												retLoadData.getState());
										bhava = true;
									}
								}
								if (!bhava) {
									// add new item
									mretRinglist.add(retLoadData);
								}
								if (mhmRunning.size() < mretRinglist.size()) {
									for (int i = 0; i < mretRinglist.size(); i++) {
										if (mhmRunning.get(mretRinglist.get(i)
												.getmOpID()) == null) {
											mretRinglist.remove(i);
										}
									}
								}
							} else if (mretRinglist != null
									&& mretRinglist.size() == 0) {
								mretRinglist.add(retLoadData);
							}
							Log.d(TAG, "the mretRinglist is ============== " + mretRinglist);
							return mretRinglist;
						} else {
							return null;
						}
					} else {
						for(int i=0;i<mretnullList.size();i++){
							Log.i(TAG, "the mretnullList name is ======== " + i +"=====" + mretnullList.get(i).getMfilename());
						}
						return mretnullList;
					}
				}
			}
		}
    
		// @Override
		public synchronized boolean isFinished(long opId) {
			return false;
		}

		// @Override
		public synchronized String getStatus(long opId) {
			return null;
		}

		// @Override
		public void setEnableNotify(boolean enable) {

			if (enable != mbNeedNotify) {
				mbNeedNotify = enable;

				if (null != msoThread) {
					msoThread.setEnableNotify(enable);
				}

				if (null != mloThread) {
					mloThread.setEnableNotify(enable);
				}
			}
		}

	}

	private class StopTimerTask extends TimerTask {
		private static final String TAG = "StopTimerTask";

		private boolean mbStop = false;

		public void toStop() {
			if (!mbStop) {
				mbStop = true;
			}
		}

		@Override
		public void run() {
			Log.i(TAG, "==> run of StopTimerTask");

			if (mbStop)
				cancel();
			else {
				if (isIdle()) {
					Log.i(TAG, "stop service now");
					stopServiceNow();
				}
			}
		}
	}

	private class ManageThread extends Thread {
		private static final String TAG = "ManageThread";

		private boolean mbStop = false;
		private Context mContext = null;
		private DownLoadServer mdls = null;

		public ManageThread(Context context) {
			if (null == context) {
				// Log.e(TAG, "context is null in ManageThread");
			} else
				mContext = context;
			mdls = DownLoadServer.getInstance(getApplicationContext());

		}

		public void toStop() {
			if (!mbStop)
				mbStop = true;
		}

		// check conflicts
		private boolean hasConflictiveOp(NetDiskOp op) {
			return false;
		}

		public synchronized void addOp(NetDiskOp op) {
			if (null == op) {
				// Log.e(TAG, "op is null in addOp");
			} else {
				mllNewOps.addLast(op);
			}
		}

		public void run() {
			while (!mbStop) {
				// Log.i(TAG, "==> run of ManageThread");

				// dispatch new ops
				if (null != mllNewOps && !mllNewOps.isEmpty()) {
					Log.i(TAG, "add new ops");

					while (true) {
						if (0 == mllNewOps.size()) {
							break;
						} else {
							NetDiskOp op = mllNewOps.removeFirst();
							// NetDiskOp op = mllNewOps.getFirst();
							if (hasConflictiveOp(op)) {
								// set result
								op.setResult("has conflictive op");
								if (op instanceof SimpleNetDiskOp) {
									((SimpleNetDiskOp) op)
											.setState(SimpleNetDiskOp.State.error);
								} else if (op instanceof LoadNetDiskOp) {
									((LoadNetDiskOp) op)
											.setState(LoadNetDiskOp.State.error);
								} else {
									// Log.e(TAG, "unknown op class");
								}
							} else {
								if (op instanceof SimpleNetDiskOp) {
									mllSimpleOps.addLast((SimpleNetDiskOp) op);
								} else if (op instanceof LoadNetDiskOp) {

									mllLoadOps.addLast((LoadNetDiskOp) op);
								} else {
									// Log.e(TAG, "unknown op class");
								}
							}
						}
					}
				}

				if (null == mloThread && !mllLoadOps.isEmpty()) {
					Log.i(TAG, "start load op service");
					mloThread = new LoadNetDiskOpThread(mContext, mllLoadOps,
							mhmDone, mhmRunning);
					mloThread.setEnableNotify(mbNeedNotify);
					mloThread.start();
				}

				if (null == msoThread && !mllSimpleOps.isEmpty()) {
					Log.i(TAG, "start simple op service");
					msoThread = new SimpleNetDiskOpThread(mContext,
							mllSimpleOps, mhmDone);
					msoThread.setEnableNotify(mbNeedNotify);
					msoThread.start();
				}

				// check whether the service should be finished
				if (isIdle()) {
					// Log.i(TAG, "in idle status");
					if (null == mttStop) {
						Log.i(TAG, "try to stop service after a while");
						mttStop = new StopTimerTask();
						Timer timer = new Timer();
						timer.schedule(mttStop, DELAY_TIME_AFTER_NO_OP);
					}
				} else {
					// Log.i(TAG, "it is busy now");
					if (null != mttStop) {
						Log.i(TAG, "cancel stop timer");
						mttStop.toStop();
						mttStop = null;
					}
				}

				// keep more time for other threads
				try {
					Thread.sleep(MANAGE_THREAD_SLEEP_TIME);
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}

			} // while
		}// run

	}

	// main data"";
	private int miBoundNum = 0;
	private boolean mbNeedNotify = false;

	// [improve] maybe use less data structure
	private LinkedList<SimpleNetDiskOp> mllSimpleOps = null;
	private LinkedList<LoadNetDiskOp> mllLoadOps = null;
	private LinkedList<NetDiskOp> mllNewOps = null;
	private HashMap<Long, NetDiskOp> mhmDone = null;
	private HashMap<Long, NetDiskOp> mhmRunning = null;
	public ArrayList<LoadedData> mretRinglist = null;
	public ArrayList<LoadedData> mretnullList = null;

	public DownLoadServer mdlServer = null;
	static String strRetNULL = "@@@##$$%%%%^^^&&&***";

	private SimpleNetDiskOpThread msoThread = null;
	private LoadNetDiskOpThread mloThread = null;
	private ManageThread mmoThread = null;

	private StopTimerTask mttStop = null;

	@Override
	public void onCreate() {
		Log.i(TAG, "==>onCreate");

		LoadedData retLoadData = new LoadedData(strRetNULL, 10, 10,
				LoadedData.tag_state.ready, LoadedData.tag_worktype.download,
				1000);

		super.onCreate();

		if (null == mllSimpleOps) {
			mllSimpleOps = new LinkedList<SimpleNetDiskOp>();
		} else {
			mllSimpleOps.clear();
		}

		if (null == mllLoadOps) {
			mllLoadOps = new LinkedList<LoadNetDiskOp>();
		} else {
			mllLoadOps.clear();
		}

		if (null == mllNewOps) {
			mllNewOps = new LinkedList<NetDiskOp>();
		} else {
			mllLoadOps.clear();
		}

		if (null == mhmDone) {
			mhmDone = new HashMap<Long, NetDiskOp>();
		} else {
			mhmDone.clear();
		}
		if (null == mhmRunning) {
			mhmRunning = new HashMap<Long, NetDiskOp>();
		} else {
			mhmRunning.clear();
		}
		if (null == mretRinglist) {
			mretRinglist = new ArrayList<LoadedData>();
		} else {
			mretRinglist.clear();
		}
		if (null == mretnullList) {
			mretnullList = new ArrayList<LoadedData>();
		} else {
			mretnullList.clear();
			
		}

		mretnullList.add(retLoadData);

		// start the manage thread
		if (null == mmoThread) {
			mmoThread = new ManageThread(this);
			mmoThread.start();
		}
	}

	@Override
	public void onDestroy() {
		Log.i(TAG, "==> onDestroy");

		super.onDestroy();

		if (null != msoThread) {
			msoThread.toStop();
			msoThread = null;
		}

		if (null != mloThread) {
			mloThread.toStop();
			msoThread = null;
		}

		if (null != mmoThread) {
			mmoThread.toStop();
			msoThread = null;
		}
	}

	@Override
	public void onRebind(Intent intent) {
		Log.i(TAG, "==>onRebind");

		super.onRebind(intent);
	}

	@Override
	public void onStart(Intent intent, int startId) {
		Log.i(TAG, "==>onStart");
		super.onStart(intent, startId);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.i(TAG, "==>onStartCommand");
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public boolean onUnbind(Intent intent) {
		Log.i(TAG, "==>onUnbind");

		--miBoundNum;
		return super.onUnbind(intent);
	}

	@Override
	public IBinder onBind(Intent intent) {
		Log.i(TAG, "==>onBind");

		++miBoundNum;
		return new NetDiskOpBinder();
	}

	private boolean isIdle() {
		if (mllSimpleOps.isEmpty() && mllLoadOps.isEmpty()
				&& mllNewOps.isEmpty() && 0 == miBoundNum
				&& mhmRunning.isEmpty() && mretRinglist.isEmpty()) {
			return true;
		} else
			return false;
	}

	private void stopServiceNow() {
		Log.i(TAG, "==> stopServiceNow");
		stopSelf();
	}

}